<script setup lang="ts">
import { get } from 'lodash-es'
import user from './composables/user'
import { qrCodeDataStore } from './stores/qrCodeData'
import { shake } from '@/utils/utils'

const qrcodeData = qrCodeDataStore()

function handleQuery(options: App.LaunchShowOption | undefined) {
    console.log('handlequery', options)
    if (options?.query?.qrCode) {
        qrcodeData.setQrCode(options.query.qrCode)
        globalVariable.qrCode = options.query.qrCode || {}
        console.log('global', options?.query?.qrCode)
        if (options?.query?.qrCode.includes('qrcodeType=normal')) {
            const data = options?.query?.qrCode.split('qrcodeType=normal&')[1]
            shake(() => {
                uni.reLaunch({
                    url: `/pages/join/join?${data}`,
                })
            })
        }
        else if (options?.query?.qrCode.includes('qrcodeType=fast-insurance&')) {
            const data = options?.query?.qrCode.split('qrcodeType=fast-insurance&')[1]
            shake(() => {
                uni.reLaunch({
                    url: `/pages/fast-insurance-status/fast-insurance-status?${data}`,
                })
            })
        }
    }
}

onLaunch(async (options) => {
    console.log('api onLaunch', options)
    handleQuery(options)
    await user.login()
    // 判断是否从scheme跳转过来（比如从微信跳过了的领薪），如果没有登录，则跳转到登录页面
    if (options?.query?.from === 'scheme' && !user.isLogin.value) {
        console.log('去登录')
        uni.navigateTo({ url: '/pages/login/login' })
    }
    // #ifdef MP-ALIPAY
    const instance = getCurrentInstance()
    const scope = get(instance, 'proxy.$scope', {}) as AnyObject
    if (typeof scope.getTabBar === 'function' && scope?.getTabBar()) {
        scope.getTabBar().setData({
            selected: 2,
        })
    }
    // #endif
})

onShow(async (options) => {
    console.log('api onShow', options)
    handleQuery(options)
})
</script>
